//
//  SearchViewController.m
//  QQ-Music
//
//  Created by lanou3g on 15/10/30.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "SearchViewController.h"
#import "AppDelegate.h"
#import "SearchHeaper.h"
#import "SerchModel.h"
#import "SDetileViewController.h"
#import "Colours.h"
#import "RecodeViewController.h"
#import "SearchTableViewCell.h"
#import "SerchDetilVC.h"
@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (weak, nonatomic) IBOutlet UIButton *searchButton;
@property (weak, nonatomic) IBOutlet UIButton *selectButton;

//用来保存搜索记录
@property(nonatomic,strong)NSMutableArray *searchArray;
@property(nonatomic,strong)NSMutableArray *dalaistarray;
@property(nonatomic,strong)SearchHeaper *searchHelsp;
@property(nonatomic,strong)NSMutableArray *arrayQ;
@end

@implementation SearchViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.searchArray = [NSMutableArray  array];
        self.dalaistarray = [NSMutableArray  array ];
        
        
        
        
    }
    return self;
}

- (void)viewDidLoad {
    self.mainTableView.dataSource = self;
    self.mainTableView.delegate = self;
    self.searchBar.delegate = self;
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
 [[SearchHeaper  shareInstace]searchWedsearchstr:self.searchBar.text withBlock:^{
     [self.mainTableView reloadData];
 }];
    [self  craest];
    
    [self.searchButton  addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainTableView  registerNib:[UINib  nibWithNibName:@"SearchTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    self.searchBar.delegate = self;
     self.dalaistarray = [NSMutableArray  arrayWithArray:self.searchArray];
    
    
    
    
    
}

//点击跳转页面
- (IBAction)selectButton:(id)sender {
    SDetileViewController *vc = [[SDetileViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}



#pragma mark searchBUtton 的点击事件

-(void)searchAction:(UIButton  *)senser{

//回收键盘
    [self.searchBar  resignFirstResponder];
    [self.searchArray  addObject:self.searchBar.text];

[[SearchHeaper shareInstace]  lodeSearchWedWithstr:self.searchBar.text withBlock:^{
   
    [self.mainTableView reloadData];

}];
    


}


-(void)craest{
    self.title = @"搜索";
    self.view.backgroundColor = [UIColor whiteColor];
    self.mainTableView.backgroundColor = [UIColor  pinkLipstickColor];

    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 18);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
    



}
#pragma mark - Button点击事件
- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [SearchHeaper shareInstace].dataArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SearchTableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"cell"];
    
    SerchModel *model =[SearchHeaper  shareInstace].dataArray[indexPath.row];
    [cell  setCell:model];
    [self.searchArray  addObject:cell.name];
//    cell.backgroundColor =    [UIColor  pinkLipstickColor];
    
   

    return cell;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//table选中状态'
    [tableView  deselectRowAtIndexPath:indexPath animated:YES];
    //获取UItableCell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.searchBar.text = cell.textLabel.text;
    SerchDetilVC  *vc = [[SerchDetilVC  alloc] init];
    [self.navigationController  pushViewController:vc animated:YES];


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;

}

#pragma mark 当搜索内容发生变化时,执行该方法,可以实现实时搜索
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

//判断搜索栏的文字不为空并且长度大于0
    if (searchText!= nil && searchText.length >0) {
        //不可变数组置为0
       self.searchArray = [NSMutableArray array ];
        //遍历存放数据的数组
        
        for (NSString  *tempStr in self.dalaistarray) {
            //判断从数组1中取出的字符串tempStr中是否有改变时的内容
            if ([tempStr  rangeOfString:searchText  options:NSCaseInsensitiveSearch].length>0) {
                //有，则将该字符串加入数组2
                [self.searchArray  addObject:tempStr];
                
            }
            
            
            
        }
        
        
        [self.mainTableView  reloadData];
        
        
    }


}

#pragma mark 取消按钮被按下,执行的方法

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    NSLog(@"%@",searchBar.text);

}

#pragma mark 键盘中,搜索按钮被按下,执行的方法
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    NSLog(@"%@",searchBar.text);
    
    // [self.rootview.searchBar resignFirstResponder];
}



@end
